<?php
//$dt_from = '2010-05-05 00:00:00';
//$dt_to = '2010-05-05 23:59:59';
	////source datetime convert functions////
	function xl2ts($xl_date){return($xl_date - 25569) * 86400;}//-3600
	function ts2xl($ts_date){return($ts_date + 25569 * 86400)/86400;}//+3600
	function add0($str){strlen($str)==1?$str='0'.$str:$str;return $str;}
	
//function xl2ts(&$xl_date){return(($xl_date - 25569)*86400 - 21600);}
//function ts2xl(&$ts_date){return($ts_date + 2209183200)/86400;}

//function dt_converter(&$xl_dt  ){return $xl_dt   = date('Y-m-d H:i:s', xl2ts($xl_dt));}
function dt_converter(&$xl_date){
	$d = new DateTime();
	$d->setTimestamp(xl2ts($xl_date));
	//$d->setTimezone(new DateTimeZone('Asia/Yekaterinburg'));
	$d->sub(new DateInterval('PT'.($d->getOffset()).'S'));

	//return $xl_date = $d->format('Y-m-d H:i:s').'|<span style="color:red">'.($d->getOffset()/3600).'</span>|';
	return $xl_date = $d->format('Y-m-d H:i:s');
}

function daysToDateTime(&$xl_date){
	$d = new DateTime();
	$d->setTimestamp(xl2ts($xl_date));
	return $d;
}

function d_converter (&$xl_date){
	$d_123 = new DateTime();
	$d_123->setTimestamp(xl2ts($xl_date));
	//$d->setTimezone(new DateTimeZone('Asia/Yekaterinburg'));
	//$d->add(new DateInterval('PT'.($d->getOffset()-14400).'S'));
	return $xl_date = $d_123->format('Y-m-d');
}
function d_conv ($xl_date){
	//$d = new DateTime();
	//$d->setTimestamp(xl2ts($xl_date));
	$t = getdate(xl2ts($xl_date));
	//$d->setTimezone(new DateTimeZone('Asia/Yekaterinburg'));
	//$d->add(new DateInterval('PT'.($d->getOffset()-14400).'S'));
	return $xl_date = array($t['wday'], $t['year'].'-'.add0($t['mon']).'-'.add0($t['mday']));
}
function t_converter (&$xl_date){
	$d = new DateTime();
	$d->setTimestamp(($xl_date*86400));
	$d->sub(new DateInterval('PT'.($d->getOffset()).'S'));
		$temp = round($xl_date*1440);
		($day = (int)($temp/1440))==0?$day='':$day.='d';
	return $xl_date = $day.' '.$d->format('H:i:s').'     ['.$temp.']';
}
function t_conv (&$xl_date){
	//$d = new DateTime();
	//$d->setTimestamp(($xl_date*86400));
	//$d->sub(new DateInterval('PT'.($d->getOffset()).'S'));
		$temp = round($xl_date*1440);
		//($day = (int)($temp/1440))==0?$day='':$day.='d';
	return $xl_date = $temp;

}
function t_conv_sec ($xl_date){
	//$d = new DateTime();
	//$d->setTimestamp(($xl_date*86400));
	//$d->sub(new DateInterval('PT'.($d->getOffset()).'S'));
		$temp = round($xl_date*86400);
		//($day = (int)($temp/1440))==0?$day='':$day.='d';
	return $temp;

}
function t_MinUp (&$xl_date){
	//$d = new DateTime();
	//$d->setTimestamp(($xl_date*86400));
	//$d->sub(new DateInterval('PT'.($d->getOffset()).'S'));
	return $xl_date = ceil($xl_date*1440);
}
function dur_converter (&$xl_time){return $xl_time = round($xl_time*86400,0);}
function dt_parse    (&$dt){
	$dt=trim($dt);
	if(strlen($dt)==10)$dt.=' 00:00:00';
	$dt_ = DateTime::createFromFormat('Y-m-d H:i:s', $dt);

	//$dt_->setTimezone(new DateTimeZone('Asia/Yekaterinburg'));
	$dt_->add(new DateInterval('PT'.($dt_->getOffset()).'S'));
	return $dt = ts2xl($dt_->getTimeStamp());
}

function callresult (&$t_, &$row_){
	$GLOBALS['stat']['count_all']['c']+=1;
	//$GLOBALS['stat']['dur_all']['c']+=$row_['SUMLENGTH'];
	if($t_!='1'){
		$GLOBALS['stat3']['withoutIVR']+=1;
		if($row_['B1WAITLENGTH']>$GLOBALS['QUALITY'])$GLOBALS['stat3']['bad']+=1;
	}
	switch($t_){
		case '1': 
			$GLOBALS['stat']['count_ivr']['c']+=1;
			break;
		case '2': 
			$GLOBALS['stat']['count_oper']['c']+=1;
			break;
		case '3': 
			$GLOBALS['stat']['count_opers']['c']+=1;
			break;
		case '4': 
			$GLOBALS['stat']['count_agents']['c']+=1;
			//$GLOBALS['stat']['dur_agents']['c']+=$row_['SUMEXTERNALLENGTH'];
			break;
		case '5': 
			$GLOBALS['stat']['count_lost']['c']+=1;
			break;
	}
}
function parserow (&$row_, &$stat_){
//$tval = $row_['SUMOPERLENGTH']*1440;
//echo '<td>',$tval,'</td><td>',ceil($tval),'</td><td>';
//( $tval-floor($tval) < 0.016666 ) ? $tval =  floor($tval) : $tval = ceil($tval);
//echo '<td>',$tval,'</td>';

//return;
	$result = $row_['CALLRESULT'];
	$stat_['count_all']+=1;
	$stat_['wait']+=$row_['B1WAITLENGTH'];
$stat_['dur_opers']+=$row_['SUMOPERLENGTH'];

//test ceil
//unset($tval);
//$tval = $row_['SUMOPERLENGTH']*1440;
//( $tval-floor($tval) < 0.016666 ) ? $stat_['dur_opersUp'] += floor($tval):$stat_['dur_opersUp'] += ceil($tval);
$stat_['dur_opersUp']+=ceil($row_['SUMOPERLENGTH']*1440);

	if($result!='1'){
		$stat_['withoutIVR']+=1;
		if($row_['B1WAITLENGTH']>$GLOBALS['QUALITY'])$stat_['bad']+=1;
	}elseif($result=='1'){
		$stat_['ivr'] += 1;
	}
	if($result=='2' || $result=='3'){
		$stat_['count_opers']+=1;
		//$stat_['dur_opers']+=$row_['SUMOPERLENGTH'];
	}
	if($result=='5'){
		if (row['SUMIVRLENGTH']>=4)
		{
			$stat_['count_lost']+=1;
		}
	}
	if($result=='4'){
			$val = $row_['SUMEXTERNALLENGTH']*1440;	
			if(preg_match('/^\+?(7|8)9/',$row_['EXNUMBER'])){
				//if($row_['ANUMBERDIALED']=='2700623')echo $row_['ANUMBERDIALED'],' сот: ',$row_['ANUMBER'],'<br/>';
				$stat_['count_agentsCell']+=1;
				( $val-floor($val) < 0.016666 )?$stat_['dur_extCell'] += floor($val):$stat_['dur_extCell'] += ceil($val);
			}else{
				//if($row_['ANUMBERDIALED']=='2700623')echo $row_['ANUMBERDIALED'],' гор: ',$row_['ANUMBER'],'<br/>';
				$stat_['count_agents']+=1;
				( $val-floor($val) < 0.016666 )?$stat_['dur_ext'] += floor($val):$stat_['dur_ext'] += ceil($val);
			}
		
	}
	/*
	switch($result){
		case '1': 
			$stat_['count_ivr']+=1;
			break;
		case '2': 
			$stat_['count_oper']+=1;
			break;
		case '3': 
			$stat_['count_opers']+=1;
			break;
		case '4': 
			$stat_['count_agents']+=1;
			break;
		case '5': 
			$stat_['count_lost']+=1;
			break;
	}
	*/
}
?>